﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DTTB.Common;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.DanhMuc;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.LinQ.DanhMuc;
using Webdiyer.WebControls.Mvc;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.DanhMucDelegate
{
    public class TrachNhiemDelegate
    {
        DBDanhMucDataContext DBDanhMuc = new DBDanhMucDataContext();

        public IList<dm_TrachNhiem> GetAllTrachNhiem()
        {
            return DBDanhMuc.dm_TrachNhiems.Where(p => p.IsDelete == false).ToList();
        }
        public dm_TrachNhiem AddNewTrachNhiem(dm_TrachNhiem TrachNhiem)
        {
            DBDanhMuc.dm_TrachNhiems.InsertOnSubmit(TrachNhiem);

            DBDanhMuc.SubmitChanges();

            return GetLoaiCongChucIdMax();
        }

        public void DeleteTrachNhiem(int TrachNhiemID)
        {
            dm_TrachNhiem TrachNhiemNew =
                DBDanhMuc.dm_TrachNhiems.Single(p => p.Id == TrachNhiemID);
            //LoaiCongChuc.Id = LoaiCongChuc.Id;

            DBDanhMuc.dm_TrachNhiems.DeleteOnSubmit(TrachNhiemNew);
            DBDanhMuc.SubmitChanges();
        }
        public int DeleteTrachNhiem(dm_TrachNhiem TrachNhiem)
        {
            dm_TrachNhiem TrachNhiemDelete =
                DBDanhMuc.dm_TrachNhiems.Single(p => p.Id == TrachNhiem.Id);
            if (TrachNhiemDelete.hs_NhanVien_TrachNhiems.Count() == 0)
            {
                TrachNhiemDelete.Id = TrachNhiem.Id;
                TrachNhiemDelete.UpdateDate = DateTime.Now;
                TrachNhiemDelete.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
                TrachNhiemDelete.IsDelete = true;
                DBDanhMuc.SubmitChanges();
                return 1;
            }
            else
            {
                return 0;
            }
        }
        public void UpdateTrachNhiem(dm_TrachNhiem TrachNhiem)
        {
            dm_TrachNhiem TrachNhiemUpdate =
                DBDanhMuc.dm_TrachNhiems.Single(p => p.Id == TrachNhiem.Id);

            TrachNhiemUpdate.Id = TrachNhiem.Id;
            TrachNhiemUpdate.TienTrachNhiem = TrachNhiem.TienTrachNhiem;
            TrachNhiemUpdate.UpdateDate = TrachNhiemUpdate.UpdateDate;
            TrachNhiemUpdate.UpdateBy = TrachNhiemUpdate.UpdateBy;
            DBDanhMuc.SubmitChanges();
        }

        public PagedList<dm_TrachNhiem> SearchTrachNhiemPagedList(int? id, string searchCriteria)// id = page Index
        {
            return (PagedList<dm_TrachNhiem>)DBDanhMuc.dm_TrachNhiems.Where(p => p.TienTrachNhiem == decimal.Parse(searchCriteria) && p.IsDelete == false).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);


        }

        public dm_TrachNhiem GetTrachNhiemById(string id)
        {
            try
            {
                dm_TrachNhiem item = this.DBDanhMuc.dm_TrachNhiems.Single(p => p.Id == Convert.ToInt32(id));
                return item;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public dm_TrachNhiem GetTrachNhiemByTenCheck(decimal ten)
        {

            IEnumerable<dm_TrachNhiem> list = this.DBDanhMuc.dm_TrachNhiems.Where(p => p.TienTrachNhiem == ten && p.IsDelete == false);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }
        public dm_TrachNhiem GetTrachNhiemByTen(decimal ten)
        {

            IEnumerable<dm_TrachNhiem> list = this.DBDanhMuc.dm_TrachNhiems.Where(p => p.TienTrachNhiem == ten);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }
        public PagedList<dm_TrachNhiem> RecycleGetTrachNhiemPagedList(int? id)// id = page Index
        {
            return DBDanhMuc.dm_TrachNhiems.Where(p => p.IsDelete == true).OrderByDescending(p => p.Id).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }
        public PagedList<dm_TrachNhiem> GetTrachNhiemPagedList(int? id)// id = page Index
        {
            return DBDanhMuc.dm_TrachNhiems.Where(p => p.IsDelete == false).OrderByDescending(p => p.Id).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }

        public dm_TrachNhiem BuildNewTrachNhiem(TrachNhiemForm form)
        {
            dm_TrachNhiem newTrachNhiem = new dm_TrachNhiem();
            if (!string.IsNullOrEmpty(form.TienTrachNhiem))
                newTrachNhiem.TienTrachNhiem = decimal.Parse(form.TienTrachNhiem.Replace(".", ""), null);
            newTrachNhiem.CreateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            newTrachNhiem.CreateDate = DateTime.Now;
            newTrachNhiem.IsDelete = false;
            return newTrachNhiem;
        }
        public int PhucHoiTrachNhiem(int id)
        {
            dm_TrachNhiem phuchoi = new dm_TrachNhiem();
            phuchoi = this.GetTrachNhiemById(Convert.ToString(id));
            dm_TrachNhiem item = new dm_TrachNhiem();
            item = this.GetTrachNhiemByTenCheck(phuchoi.TienTrachNhiem.Value);
            if (item != null)
                return 1;
            else
            {
                phuchoi.IsDelete = false;
                DBDanhMuc.SubmitChanges();
                return 2;
            }
        }
        public dm_TrachNhiem BuildEditTrachNhiem(TrachNhiemForm form)
        {
            dm_TrachNhiem newTrachNhiem = new dm_TrachNhiem();
            newTrachNhiem.Id = form.id;
            if (!string.IsNullOrEmpty(form.TienTrachNhiem))
                newTrachNhiem.TienTrachNhiem = decimal.Parse(form.TienTrachNhiem.Replace(".", ""), null);
            newTrachNhiem.UpdateDate = DateTime.Now;
            newTrachNhiem.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            return newTrachNhiem;
        }
        public dm_TrachNhiem GetLoaiCongChucIdMax()
        {
            IList<dm_TrachNhiem> list = DBDanhMuc.dm_TrachNhiems.ToList();
            return list.Count != 0 ? list.ElementAt(list.Count - 1) : null;
        }
        public TrachNhiemForm ConvertToForm(dm_TrachNhiem obj)
        {
            TrachNhiemForm form = new TrachNhiemForm();
            form.id = obj.Id;
            form.TienTrachNhiem = obj.TienTrachNhiem != null ? obj.TienTrachNhiem.Value.ToString("#,#").Replace(",", ".") : "";
            form.UpdateDate = obj.UpdateDate != null ? obj.UpdateDate.Value : DateTime.MinValue;
            form.CreateDate = obj.CreateDate != null ? obj.CreateDate.Value : DateTime.MinValue;
            form.UpdateBy = obj.UpdateBy != null ? obj.UpdateBy.Value : 0;
            form.CreateBy = obj.CreateBy != null ? obj.CreateBy.Value : 0;
            return form;
        }
    }
}